﻿local addon, DB = ...

local cache = setmetatable({}, {__mode = "k"})

local DROP_TYPE = {
	N = "",
	H = " (Héroïque)",
	N1 = " (Raid 10)",
	N2 = " (Raid 25)",
	H1 = " (Raid 10 héroïque)",
	H2 = " (Raid 25 héroïque)",
}

local MULTIPLE_BOSS = "Boss multiples"
local MULTIPLE_ZONES = "Zones multiples"
local SOLD_FOR_PATTERN = "Acheté contre %d |T%s:18|t"
local ZONE_PATTERN = "|cffB0C4DE%s%s"

local function OnTooltipSetItem(tooltip)
	if cache[tooltip] then return end
	cache[tooltip] = true
	local _, itemLink = tooltip:GetItem()
	if not itemLink then return end
	local id = tonumber(itemLink:match("item:(%d+)"))
	local zone = DB.zones[id]
	zone = zone == "M" and MULTIPLE_ZONES or zone
	if zone then
		local boss = DB.bosses[id]
		boss = boss == "M" and MULTIPLE_BOSS or boss
		local type = DROP_TYPE[DB.drop_types[id]]
		tooltip:AddDoubleLine(ZONE_PATTERN:format(zone, type), boss)
	end
	local currency = DB.currencies[id]
	if currency then
		local cost = DB.costs[id]
		local _, _, _, _, _, _, _, _, _, texture = GetItemInfo(currency)
		tooltip:AddLine(SOLD_FOR_PATTERN:format(cost, texture))
	end
end

local function OnTooltipCleared(tooltip)
	cache[tooltip] = nil
end

local function hook(tooltip)
	tooltip:HookScript("OnTooltipSetItem", OnTooltipSetItem)
	tooltip:HookScript("OnTooltipCleared", OnTooltipCleared)
end

hook(GameTooltip)
hook(ShoppingTooltip1)
hook(ShoppingTooltip2)
hook(ItemRefTooltip)
